//logs.js
var util = require('../../utils/util.js')
Page({
  data: {
    item : {
       display     : 'hide',
       error_info  : '操作太过频繁',
    },
    navigator: {
      nav_one: ['/image/nav3.png','nav1'],
      nav_two: ['/image/nav5.png',''],
      nav_three: ['/image/nav2.png',''],
      nav_type:'/pages/battle/battle'
    },
    battle_id      : '',
    player1_name   : '丁 先生',
    player2_name   : '奥 先生',
    player1_name_2 : '',
    player2_name_2 : '',
    player1_score  : '000',
    player2_score  : '000',
    sum_frame      : 7,
    last_time      : '',
    event_name     : '赛事名称',
    event_stage    : '比赛阶段',
    button_1 : {
      default_btn  : '/image/action_btn.png'  
    },
    button_2 : {
      default_btn  : '/image/action_btn.png'
    }
  },
  //分享
  onShareAppMessage: function () {
      return {
          title: wx.getStorageSync('wxapp_config').share_title,
          path: '/pages/index/index',
          success: function (res) {
       
          },
          fail: function (res) {
      
          }
      }
  },

  //保存对局的信息到本地缓存
  saveBattleInfo: function (battle_id) {
    var that = this;
    wx.setStorage({
      key: "battle_info",
      data: {
        battle_id: battle_id,
        event_stage: that.data.event_stage,
        sum_frame: that.data.sum_frame,
        player1_name: that.data.player1_name,
        player2_name: that.data.player2_name,
        battle_type:1
      }
    });

    wx.setStorage({
      key: 'player1_score',
      data: '000'
    });

    wx.setStorage({
      key: 'player2_score',
      data: '000'
    });
  } ,

  //获取对阵的信息
  onShow:function(){
      var that = this; 
      var battle_info = wx.getStorageSync('battle_info');
      var nav_type = battle_info.battle_type == 2 ? '/pages/snookerbattle/snookerbattle' : '/pages/battle/battle';
      this.setData({ navigator: { nav_type: nav_type } });
      if(battle_info){
         this.setData({
             battle_id      : battle_info.battle_id,
             player1_name   : battle_info.player1_name,
             player2_name   : battle_info.player2_name,
             event_stage    : battle_info.event_stage,
             player1_score  : wx.getStorageSync('player1_score'),
             player2_score  : wx.getStorageSync('player2_score'),
             sum_frame      : battle_info.sum_frame,
             last_time      : Date.now()-1000,
             event_name     : wx.getStorageSync('event_info').event_name
         })
        //双打判断根据选手姓名中是否有逗号判断
        if (battle_info.player1_name.indexOf(',')>0){
             var player1_name_array = battle_info.player1_name.split(",");
             this.setData({
                  player1_name   : player1_name_array[0],
                  player1_name_2 : player1_name_array[1]
             })
         } else if (battle_info.player1_name.indexOf('，')>0){
             var player1_name_array = battle_info.player1_name.split("，");
             this.setData({
                  player1_name   : player1_name_array[0],
                  player1_name_2 : player1_name_array[1]
             })
         }
         
         if (battle_info.player2_name.indexOf(',')>0){
             var player2_name_array = battle_info.player2_name.split(",");
             this.setData({
                  player2_name   : player2_name_array[0],
                  player2_name_2 : player2_name_array[1]
             })
         }else if(battle_info.player2_name.indexOf('，')>0){
             var player2_name_array = battle_info.player2_name.split("，");
             this.setData({
                  player2_name   : player2_name_array[0],
                  player2_name_2 : player2_name_array[1]
             })
         }
      }else{
        console.log(111);
        var battle_info = wx.getStorageSync('battle_info') || '';
        if (battle_info == '') {
          console.log(111);
          var url = getApp().data.servser + '/Battle/add';
          wx.request({
            url: url,
            data: {
              'event_stage': '半决赛',
              'sum_frame': 7,
              'player1_name': '丁 先生',
              'player2_name': '奥 先生',
              'event_id': wx.getStorageSync('event_id'),
              'battle_id': ''
            },
            header: { 'content-type': 'application/json' },
            method: "GET",

            success: function (res) {
              wx.hideLoading();
              if (res.data.state == 1) {
                //将对局信息保存在本地缓存中
                that.saveBattleInfo(res.data.id);
                wx.setStorage({
                  key: 'battle_type',
                  data: 1
                });
              }
            }
          })
        }
      }
  },

 //跳转到信息编辑的页面
  toEditbattle:function(){
      if(this.remindAddBattleInfo()){
         wx.navigateTo({
            url: '/pages/editbattle/editbattle'
         })
      }
  },

  //用户按下红角选手的加分按钮
  jia1ButtonTouchStart:function(){
      this.setData({ button_1:{default_btn  : '/image/jia_btn.png'}})
  },

  //用户松开加分按钮
  jia1ButtonTouchEnd:function(){
    if (wx.vibrateShort) {
      wx.vibrateShort({});
    }
      this.setData({ button_1: { default_btn: '/image/action_btn.png'}})
      if(this.preventFastOperation()){
          var player1_score = Number(this.data.player1_score)+1;
              player1_score = player1_score > 99 ? 99 : player1_score;
          this.saveScore(1,player1_score);
      }
  },

  //用户按下红角选手的减分按钮
  jian1ButtonTouchStart:function(){
      this.setData({ button_1:{default_btn  : '/image/jian_btn.png'}})
  },

  //用户松开红角选手的减分按钮
  jian1ButtonTouchEnd:function(){
    if (wx.vibrateShort) {
      wx.vibrateShort({});
    }
      this.setData({ button_1: {default_btn  : '/image/action_btn.png'}})
      if(this.preventFastOperation()){
          var player1_score = Number(this.data.player1_score)-1;
              player1_score = player1_score < 0 ? 0 : player1_score;
          this.saveScore(1,player1_score);
      }
  },

  jia2ButtonTouchStart:function(){
      this.setData({ button_2:{default_btn  : '/image/jia_btn.png'}})
  },

  jia2ButtonTouchEnd:function(){
    if (wx.vibrateShort) {
      wx.vibrateShort({});
    }
      this.setData({ button_2:{default_btn: '/image/action_btn.png'}})
      if(this.preventFastOperation()){
          var player2_score = Number(this.data.player2_score)+1;
          player2_score = player2_score > 99 ? 99 : player2_score;
          this.saveScore(2,player2_score);
       }
    
  },

  jian2ButtonTouchStart:function(){
      this.setData({ button_2:{default_btn: '/image/jian_btn.png'}})
  },

  jian2ButtonTouchEnd:function(){
    if (wx.vibrateShort) {
      wx.vibrateShort({});
    }
      this.setData({ button_2:{default_btn: '/image/action_btn.png'}})
      if(this.preventFastOperation()){
          var player2_score = Number(this.data.player2_score)-1;
              player2_score = player2_score < 0 ? 0 : player2_score;
          this.saveScore(2,player2_score);
      }
  },
  //修改比分
  saveScore:function(player,score){
      //验证分数是否达到最大值，如果分数达到最大值不能进行加分
      if(!this.checkScore(score)){
          return false;
      }
      if(score < 10) {score = '00'+score};
      if(score > 9)  {score =  '0'+score};
      var that = this;

      
      //设置页面比分显示与修改本地缓存
      switch(player){
         case 1:this.setData({
             player1_score   : score,
         });wx.setStorage({
             key: 'player1_score',
             data: score
         });break;
         case 2:this.setData({
             player2_score   : score,
         });wx.setStorage({
             key: 'player2_score',
             data: score
        });break;
      }

      //修改远程比分
      var url  = getApp().data.servser+'/Battle/edit_score';
      wx.request({
          url: url,
          data: {
              'battle_id'       : that.data.battle_id,
              'player1_score'   : Number(that.data.player1_score),
              'player2_score'   : Number(that.data.player2_score),
          },
          header: {
              'content-type': 'application/json'
          },
          method : "GET",
          success: function(res) {
              
          }
      })
  },
  //防止用户操作过快
  preventFastOperation:function(){
      var that =this;
      var now_time = Date.now();
      if(this.remindAddBattleInfo()){
          if(now_time-this.data.last_time>500){
              this.data.last_time = now_time;
              return true;
          }else{
              that.setData({
                  item: {
                      display    :'show',
                      error_info :'操作太过频繁'
                  }
              })
              this.closeErrorInfo();
              return false;
          }          
      }else{
          return false;
      } 
   },

    //定时器关闭提示信息
  closeErrorInfo:function(){
      var that = this;
      setTimeout(function(){
          that.setData({ item: {display:'hide'} })
      },'500')
  },
    //如果对局信息为空提示用户添加对局信息
  remindAddBattleInfo:function(){
      var that = this;
      var battle_info = wx.getStorageSync('battle_info');
      if(!battle_info){
          that.setData({
              item: {
                 display    :'show',
                 error_info :'请添加比赛信息'
              }
          })
          this.closeErrorInfo();
          return false; 
      }else{
            return true;
      }
  },
  //验证比分是否达到最大值
  checkScore:function(score){
      //获取比赛的总局数
    var that      = this; 
    var sum_frame = Number(this.data.sum_frame);
    var big_score = Math.ceil((sum_frame+1)/2);
    var player1_score = Number(this.data.player1_score);
    var player2_score = Number(this.data.player2_score);
  
    if ((player1_score >= big_score && score >= big_score) || (player2_score >= big_score && score >= big_score) || score > big_score){
      that.setData({
          item: {
            display: 'show',
            error_info: '比分已达到最大值'
          }
      })
      this.closeErrorInfo();
      return false;
    }else{
       return true;
    }
  }
})
